# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  # Grabbed this from the great project management application "Redmine" because I couldn't find a better method online.
  # Really, thanks for the progress_bar goes to them.
  def progress_bar(pcts, options={})
    pcts = [pcts, pcts] unless pcts.is_a?(Array)
    pcts[1] = pcts[1] - pcts[0]
    pcts << (100 - pcts[1] - pcts[0])
    width = options[:width] || '100px;'
    legend = options[:legend] || ''
    content_tag('table',
      content_tag('tr',
        (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') +
        (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') +
        (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '')
      ), :class => 'progress', :style => "width: #{width};") +
      content_tag('p', legend, :class => 'percent')
  end

  def switch_sort_order
    return params[:order] == "DESC" ? "ASC" : "DESC"
  end
  
  def get_sort_link(title, field)
    html_options =  {
      :title => "Sort by "+title,
      :href => url_for(:action => 'index', :params => {:sort => field, :order => self.switch_sort_order})
    }
    
    x = link_to(title, {}, html_options) + " " + self.get_image_tag(field)
    return x
  end

  def get_image_tag(field)
    if params[:sort] == field
      return params[:order] == "DESC" ? image_tag("arrowDown.png", :border => 0, :align => "texttop") : image_tag("arrowUp.png", :border => 0, :align => "texttop")
    else
      return ""
    end
  end
  
end
